<template>
  <div class="smart-port-page">
    <div class="smart-port-content">
      <!-- UE固定尺寸容器 -->
      <div class="ue-container">
        <iframe 
          :src="ueConfig.streamUrl" 
          class="ue-iframe"
          frameborder="0"
          allowfullscreen
        ></iframe>

        <!-- 顶部信息栏 -->
        <div class="top-info-bar">
          <div class="left-info">
            <div class="datetime">{{ currentDateTime }}</div>
          </div>
          <div class="center-title">
            <h1 class="main-title">智慧港口数据大屏</h1>
          </div>
          <div class="right-info">
            <div class="weather">☁️ 37℃ PM: 40 µg/m³</div>
          </div>
        </div>

        <!-- 左侧数据面板 -->
        <div class="left-panel">
          <!-- 入港时刻 - 流程图 -->
          <div class="panel-section">
            <h3 class="section-title">🚢 入港时刻</h3>
            <div class="port-flow">
              <div class="flow-item">
                <div class="flow-number">54</div>
                <div class="flow-label">即将入港</div>
              </div>
              <div class="flow-arrow">→</div>
              <div class="flow-center">
                <div class="port-icon">🚢</div>
              </div>
              <div class="flow-arrow">→</div>
              <div class="flow-item">
                <div class="flow-number">66</div>
                <div class="flow-label">即将离岸</div>
              </div>
            </div>
          </div>

          <!-- 船舶列表 -->
          <div class="panel-section">
            <div class="ship-list">
              <div class="ship-item" v-for="(ship, index) in shipData" :key="index">
                <div class="ship-visual">
                  <div class="ship-image">{{ ship.icon }}</div>
                  <div class="ship-category">{{ ship.category }}</div>
                </div>
                <div class="ship-details">
                  <div class="ship-number">{{ ship.number }}</div>
                  <div class="ship-location">📍 {{ ship.location }}</div>
                  <div class="ship-time">{{ ship.time }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧数据面板 -->
        <div class="right-panel">
          <!-- 本月船舶到港情况 -->
          <div class="panel-section">
            <h3 class="section-title">本月船舶到港情况</h3>
            <div class="port-stats">
              <div class="stats-header">
                <div class="today-permit">
                  <div class="permit-icon">⚓</div>
                  <div class="permit-text">今日共计到港</div>
                  <div class="permit-number">51112</div>
                </div>
              </div>
              <div class="ship-types">
                <div class="type-item" v-for="type in shipTypes" :key="type.name">
                  <div class="type-info">
                    <span class="type-name">{{ type.name }}</span>
                    <span class="type-count">{{ type.count }}艘</span>
                  </div>
                  <div class="type-status">{{ type.status }}</div>
                </div>
              </div>
            </div>
          </div>

          <!-- 本月吞吐情况 -->
          <div class="panel-section">
            <h3 class="section-title">本月吞吐情况</h3>
            <div class="throughput-chart">
              <div class="chart-legend">
                <span class="legend-item blue">进港吞吐量</span>
                <span class="legend-item green">出港吞吐量</span>
              </div>
              <div class="line-chart">
                <svg width="100%" height="120" viewBox="0 0 280 120">
                  <!-- 网格线 -->
                  <defs>
                    <pattern id="grid" width="35" height="24" patternUnits="userSpaceOnUse">
                      <path d="M 35 0 L 0 0 0 24" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="1"/>
                    </pattern>
                  </defs>
                  <rect width="100%" height="100%" fill="url(#grid)" />
                  
                  <!-- 进港数据线 -->
                  <polyline 
                    :points="inboundLine" 
                    fill="none" 
                    stroke="#00f2fe" 
                    stroke-width="2"
                  />
                  <!-- 出港数据线 -->
                  <polyline 
                    :points="outboundLine" 
                    fill="none" 
                    stroke="#4facfe" 
                    stroke-width="2"
                  />
                  
                  <!-- 数据点 -->
                  <circle 
                    v-for="(point, index) in chartPoints.inbound" 
                    :key="'in-' + index"
                    :cx="point.x" 
                    :cy="point.y" 
                    r="3" 
                    fill="#00f2fe"
                  />
                  <circle 
                    v-for="(point, index) in chartPoints.outbound" 
                    :key="'out-' + index"
                    :cx="point.x" 
                    :cy="point.y" 
                    r="3" 
                    fill="#4facfe"
                  />
                </svg>
                
                <!-- X轴标签 -->
                <div class="chart-labels">
                  <span v-for="month in 8" :key="month" class="month-label">{{ month }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from "vue";

// UE配置参数
const ueConfig = ref({
  // UE服务地址
  streamUrl: 'http://127.0.0.1:11192/video.html'
});

// 响应式状态
const currentDateTime = ref('');

// 船舶数据
const shipData = ref([
  { icon: '🚢', category: '货轮—上海', number: 'xxxxxxx船1号', location: '东区入港', time: '2025-09-19 20:57:58' },
  { icon: '⛴️', category: '客轮—上海', number: 'xxxxxxx船1号', location: '东区入港', time: '2025-09-19 20:57:58' },
  { icon: '🚢', category: '货轮—上海', number: 'xxxxxxx船1号', location: '东区入港', time: '2025-09-19 20:57:58' },
  { icon: '⛴️', category: '客轮—上海', number: 'xxxxxxx船1号', location: '东区入港', time: '2025-09-19 20:57:58' },
  { icon: '🚢', category: '货轮—上海', number: 'xxxxxxx船1号', location: '东区入港', time: '2025-09-19 20:57:58' }
]);

// 船舶类型统计
const shipTypes = ref([
  { name: '国家/地区', count: '起运港', status: '到达港' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' },
  { name: '英国/伦敦', count: '伦敦', status: '宁波' }
]);

// 吞吐量数据
const throughputData = ref({
  inbound: [600, 700, 650, 800, 750, 900, 850, 950],
  outbound: [500, 600, 550, 700, 650, 800, 750, 850]
});

// 计算图表线条路径
const inboundLine = computed(() => {
  return throughputData.value.inbound.map((value, index) => {
    const x = 35 + index * 35;
    const y = 100 - (value / 1000) * 80;
    return `${x},${y}`;
  }).join(' ');
});

const outboundLine = computed(() => {
  return throughputData.value.outbound.map((value, index) => {
    const x = 35 + index * 35;
    const y = 100 - (value / 1000) * 80;
    return `${x},${y}`;
  }).join(' ');
});

// 计算数据点位置
const chartPoints = computed(() => {
  return {
    inbound: throughputData.value.inbound.map((value, index) => ({
      x: 35 + index * 35,
      y: 100 - (value / 1000) * 80
    })),
    outbound: throughputData.value.outbound.map((value, index) => ({
      x: 35 + index * 35,
      y: 100 - (value / 1000) * 80
    }))
  };
});

// 更新时间
const updateDateTime = () => {
  const now = new Date();
  const options = { 
    year: 'numeric', 
    month: '2-digit', 
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    weekday: 'long'
  };
  currentDateTime.value = now.toLocaleString('zh-CN', options);
};

onMounted(() => {
  console.log('智慧港口页面已加载，UE数据流iframe已就绪');
  console.log('UE配置:', ueConfig.value);
  
  updateDateTime();
  setInterval(updateDateTime, 1000);
});

onUnmounted(() => {
  console.log('智慧港口页面已卸载');
});
</script>

<style scoped>
/* 重置所有可能的margin和padding */
.smart-port-page * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.smart-port-page {
  width: 100%;
  height: 100%;
  background: #e3f2fd;
  color: #333;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  font-family: "Microsoft YaHei", sans-serif;
  overflow: hidden;
}

.smart-port-content {
  width: 100%;
  height: 100%;
  position: relative;
  margin: 0;
  padding: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ue-container {
  width: 100%;
  height: 100%;
  position: relative;
  margin: 0;
  padding: 0;
  max-width: 1920px;
  max-height: 1080px;
}

.ue-iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  margin: 0;
  padding: 0;
  object-fit: fill;
  /* 强制覆盖iframe样式，消除黑边 */
  transform: scale(1.05);
  transform-origin: center center;
}

/* 顶部信息栏 */
.top-info-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 80px;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  z-index: 10;
  border-bottom: 2px solid #00f2fe;
}

.left-info, .right-info {
  color: #00f2fe;
  font-size: 16px;
  font-weight: 500;
}

.center-title {
  flex: 1;
  text-align: center;
}

.main-title {
  color: #00f2fe;
  font-size: 32px;
  font-weight: bold;
  margin: 0;
  text-shadow: 0 0 20px #00f2fe;
  letter-spacing: 2px;
}

/* 左右面板通用样式 */
.left-panel, .right-panel {
  position: absolute;
  top: 100px;
  width: 300px;
  height: calc(100% - 130px);
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 242, 254, 0.3);
  border-radius: 8px;
  padding: 15px;
  z-index: 5;
}

.left-panel {
  left: 20px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.right-panel {
  right: 20px;
}

.panel-section {
  margin-bottom: 20px;
}

.panel-section:first-child {
  min-height: 120px; /* 流程图部分较小 */
}

.panel-section:last-child {
  flex: 1; /* 船舶列表占剩余空间 */
  min-height: 250px;
  display: flex;
  flex-direction: column;
}

.section-title {
  color: #00f2fe;
  font-size: 18px;
  font-weight: bold;
  margin: 0 0 15px 0;
  text-align: center;
  border-bottom: 1px solid rgba(0, 242, 254, 0.3);
  padding-bottom: 10px;
}

/* 港口流程 */
.port-flow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 10px;
  background: rgba(0, 242, 254, 0.05);
  border-radius: 12px;
  border: 1px solid rgba(0, 242, 254, 0.2);
}

.flow-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.flow-number {
  color: #00f2fe;
  font-size: 32px;
  font-weight: bold;
  text-shadow: 0 0 15px #00f2fe;
  margin-bottom: 5px;
}

.flow-label {
  color: #ffffff;
  font-size: 12px;
  font-weight: 500;
}

.flow-arrow {
  color: #00f2fe;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 0 10px #00f2fe;
}

.flow-center {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  background: rgba(0, 242, 254, 0.2);
  border: 2px solid #00f2fe;
  border-radius: 50%;
  box-shadow: 0 0 20px rgba(0, 242, 254, 0.3);
}

.port-icon {
  color: #00f2fe;
  font-size: 28px;
  text-shadow: 0 0 10px #00f2fe;
}

/* 船舶列表 */
.ship-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  overflow-y: auto;
  height: 100%;
}

.ship-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: rgba(0, 242, 254, 0.08);
  border-radius: 8px;
  border: 1px solid rgba(0, 242, 254, 0.2);
  transition: all 0.3s ease;
}

.ship-item:hover {
  background: rgba(0, 242, 254, 0.15);
  border-color: rgba(0, 242, 254, 0.4);
}

.ship-visual {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60px;
  text-align: center;
}

.ship-image {
  font-size: 32px;
  margin-bottom: 4px;
  filter: drop-shadow(0 0 8px rgba(0, 242, 254, 0.5));
}

.ship-category {
  color: #ffffff;
  font-size: 10px;
  opacity: 0.8;
}

.ship-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.ship-number {
  color: #00f2fe;
  font-size: 14px;
  font-weight: bold;
}

.ship-location {
  color: #ffffff;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.ship-time {
  color: #ffffff;
  font-size: 11px;
  opacity: 0.7;
}

/* 港口统计 */
.port-stats {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.stats-header {
  text-align: center;
  padding: 15px;
  background: rgba(0, 242, 254, 0.1);
  border-radius: 8px;
  border: 1px solid rgba(0, 242, 254, 0.3);
}

.today-permit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 15px;
  background: rgba(0, 100, 200, 0.3);
  border-radius: 20px;
  border: 1px solid rgba(0, 242, 254, 0.5);
}

.permit-icon {
  color: #00f2fe;
  font-size: 20px;
}

.permit-text {
  color: #ffffff;
  font-size: 14px;
  font-weight: 500;
}

.permit-number {
  color: #00f2fe;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 0 10px #00f2fe;
}

/* 船舶类型 */
.ship-types {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.type-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: rgba(0, 242, 254, 0.05);
  border-radius: 4px;
  border: 1px solid rgba(0, 242, 254, 0.1);
}

.type-info {
  display: flex;
  justify-content: space-between;
  flex: 1;
  margin-right: 10px;
}

.type-name {
  color: #ffffff;
  font-size: 12px;
}

.type-count {
  color: #00f2fe;
  font-size: 12px;
  font-weight: bold;
}

.type-status {
  color: #ffffff;
  font-size: 12px;
}

/* 吞吐量图表 */
.throughput-chart {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chart-legend {
  display: flex;
  justify-content: center;
  gap: 20px;
  font-size: 12px;
  margin-bottom: 10px;
}

.legend-item {
  position: relative;
  padding-left: 15px;
  color: #ffffff;
}

.legend-item.blue::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 2px;
  background: #00f2fe;
}

.legend-item.green::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 2px;
  background: #4facfe;
}

.line-chart {
  position: relative;
  background: rgba(0, 242, 254, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(0, 242, 254, 0.2);
  padding: 10px;
}

.chart-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  padding: 0 35px;
}

.month-label {
  color: #ffffff;
  font-size: 10px;
  text-align: center;
  flex: 1;
}

/* 响应式设计 */
.ue-container {
  /* 确保容器不会超出父容器 */
  box-sizing: border-box;
}

/* 在小屏幕上保持16:9比例 */
@media (max-width: 1920px) or (max-height: 1080px) {
  .ue-container {
    aspect-ratio: 16/9;
    width: 100%;
    height: 100%;
    max-height: 100%;
  }
}
</style>